package com.zhiyou.app.shop.website.action;

import java.io.File;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.zhiyou.app.shop.common.BaseAction;
import com.zhiyou.app.shop.website.util.bak.DataFile;

public class DataAction extends BaseAction implements ServletRequestAware {

	private static final long serialVersionUID = -6304689065610370559L;
	private String dbname = "aycshop";
	private List<DataFile> dataFiles = new ArrayList<DataFile>();
	private File reductionFile;
	
	private String username= "root";
	private String password = "123qweasd";

	public File getReductionFile() {
		return reductionFile;
	}

	public void setReductionFile(File reductionFile) {
		this.reductionFile = reductionFile;
	}

	public List<DataFile> getDataFiles() {
		return dataFiles;
	}

	public void setDataFiles(List<DataFile> dataFiles) {
		this.dataFiles = dataFiles;
	}

	public String getDbname() {
		return dbname;
	}

	public void setDbname(String dbname) {
		this.dbname = dbname;
	}

	/**
	 * 删除
	 * */
	public String delete() {
		String fileName = request.getParameter("fileName");
		System.out.println(fileName);
		String backPath = ServletActionContext.getServletContext().getRealPath(
				"/")
				+ "/back/" + fileName;
		File file = new File(backPath);
		file.delete();
		return "success";
	}

	/**
	 * 得到备份文件的List集合
	 * 
	 * */
	public String findList() {
		String backPath = ServletActionContext.getServletContext().getRealPath("/back/");
		File file = new File(backPath);
		if (!file.exists())
			return "findListData";
		File[] file1 = file.listFiles();
		for (int i = 0; i < file1.length; i++) {
			if (file1[i].getName().equals("ramdit.txt"))
				continue;
			SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
			// 前面的lSysTime是秒数，先乘1000得到毫秒数，再转为java.util.Date类型
			java.util.Date dt = new Date(file1[i].lastModified());
			String sDateTime = sdf.format(dt); // 得到精确到秒的表示：08/31/2006 21:08:00
			DataFile dataFile = new DataFile();
			dataFile.setFileName(file1[i].getName());
			dataFile.setFileDate(sDateTime);
			String path = request.getContextPath();
			String filePath = request.getScheme() + "://"
					+ request.getServerName() + ":" + request.getServerPort()
					+ path + "/back/" + file1[i].getName();
			dataFile.setFilePath(filePath);
			DecimalFormat df = new DecimalFormat(".## ");
			dataFile.setFileSize(df.format(file1[i].length() / 1024000f));
			dataFiles.add(dataFile);
		}
		return "findListData";
	}

	/**
	 * 备份
	 * */
	public String backup() {
		// 得到配置文件
		try {
			Runtime rt = Runtime.getRuntime();
			String backPath = ServletActionContext.getServletContext()
					.getRealPath("/")
					+ "back/"
					+ System.currentTimeMillis() + ".sql";
			String mysql = "mysqldump -u" + username + " -p"
					+ password + " --default-character-set=utf8 " + dbname + " >"
					+ "\"" + backPath + "\"";
			Process proc = rt.exec(new String[] { "sh", "-c",mysql });// 设置导出编码为utf8。这里必须是utf8
			int tag = proc.waitFor();// 等待进程终止
			System.out.println(tag);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}

	/**
	 * 还原
	 * */
	public String load() {
		String sqlPath = "";
		if (request.getParameter("selectName") != null)
			sqlPath = request.getParameter("selectName");
		
		if (sqlPath.substring(sqlPath.lastIndexOf(".") + 1).equals("sql")) {
			// 得到配置文件
			try {
				Runtime rt = Runtime.getRuntime();
//				String createDb = "mysqladmin -u" + username + " -p"
//						+ password + " create " + dbname;
				String mysql = "mysql -u" + username + " -p"
						+ password + " " + dbname + " <" + "\""
						+ sqlPath + "\"";// +"\""+backPath+"\""
				//rt.exec("cmd.exe /c " + createDb);
				Process proc = rt.exec(new String[] { "sh", "-c", mysql });
				int tag = proc.waitFor();// 等待进程终止
				System.out.println(tag);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "success";
	}

	protected HttpServletRequest request;
	
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
}
